package com.psa.element;

import android.content.ContentValues;
import android.database.Cursor;
import android.graphics.Color;
import android.view.Gravity;
import com.psa.system.R;
import com.psa.tool.DBTool;
import com.psa.tool.SystemStatus;
import com.wxw.BaseLibrary.element.DataSourceColumn;
import com.wxw.BaseLibrary.element.IDataGridViewDataSource;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author david
 * @date 2014-09-02
 * [BillNo]
 ,[BillDate]
 ,[CustomerID]
 ,[Amount]
 ,[isCredit]
 ,[OrderDate]
 ,[StatisticsCode1]
 ,[StatisticsTime1]
 */
public class OrderInvoice implements ISqlObject,IDataGridViewDataSource {
    public int BillNo = 0;
    public String BillDate = "";
    public String CustomerID = "";
    public double Amount = 0.0;
    public boolean isCredit = false;
    public String OrderDate = "";
    public String StatisticsCode1 = "";
    public String StatisticsTime1 = "";
    public double Count = 0.0;
    public double Sum = 0.0;
    public int Index = -1;
    public int Days = 3;
    public List<OrderInvoiceItem> items = new ArrayList<OrderInvoiceItem>();
    public List<DataSourceColumn> columns = new ArrayList<DataSourceColumn>();

    public OrderInvoice(){
        columns.add(new DataSourceColumn(SystemStatus.MainActivity.getString(R.string.order_col_code), Gravity.LEFT | Gravity.CENTER_VERTICAL, "", 15));
        columns.add(new DataSourceColumn(SystemStatus.MainActivity.getString(R.string.order_col_name), Gravity.LEFT | Gravity.CENTER_VERTICAL, "", 35));
        columns.add(new DataSourceColumn(SystemStatus.MainActivity.getString(R.string.order_col_qty), Gravity.RIGHT | Gravity.CENTER_VERTICAL, "", 15));
        columns.add(new DataSourceColumn(SystemStatus.MainActivity.getString(R.string.order_col_type), Gravity.CENTER, "", 10));
        columns.add(new DataSourceColumn(SystemStatus.MainActivity.getString(R.string.order_col_price), Gravity.RIGHT | Gravity.CENTER_VERTICAL, "##0.00", 15));
        columns.add(new DataSourceColumn(SystemStatus.MainActivity.getString(R.string.order_col_package), Gravity.CENTER, "", 10));
    }

    @Override
    public int insert(DBTool dbTool) {
        ContentValues values = new ContentValues();
        values.put("BillNo", BillNo);
        values.put("BillDate", BillDate);
        values.put("CustomerID", CustomerID);
        values.put("Amount", Amount);
        values.put("isCredit", isCredit);
        values.put("OrderDate", OrderDate);
        values.put("StatisticsCode1", StatisticsCode1);
        values.put("StatisticsTime1", StatisticsTime1);
        return (int)dbTool.insert("OrderInvoice", values);
    }

    @Override
    public int update(DBTool dbTool) {
        ContentValues values = new ContentValues();
        values.put("BillDate", BillDate);
        values.put("CustomerID", CustomerID);
        values.put("Amount", Amount);
        values.put("isCredit", isCredit);
        values.put("OrderDate", OrderDate);
        values.put("StatisticsCode1", StatisticsCode1);
        values.put("StatisticsTime1", StatisticsTime1);
        String where = "BillNo = ?";
        String[] whereval = { String.valueOf(BillNo) };
        return dbTool.update("OrderInvoice", values, where, whereval);
    }

    @Override
    public int delete(DBTool dbTool) {
        String where = "BillNo = ?";
        String[] val = { String.valueOf(BillNo) };
        return dbTool.delete("OrderInvoice", where, val);
    }

    @Override
    public ISqlObject full(Cursor cursor) {
        if (cursor!=null && cursor.moveToNext()) {
            OrderInvoice orderInvoice = new OrderInvoice();
            orderInvoice.BillNo = cursor.getInt(0);
            orderInvoice.BillDate = cursor.getString(1);
            orderInvoice.CustomerID = cursor.getString(2);
            orderInvoice.Amount = cursor.getFloat(3);
            orderInvoice.isCredit = cursor.getInt(4) == 1;
            orderInvoice.OrderDate = cursor.getString(5);
            orderInvoice.StatisticsCode1 = cursor.getString(6);
            orderInvoice.StatisticsTime1 = cursor.getString(7);
            return orderInvoice;
        }
        return null;
    }

    @Override
    public void clear(DBTool dbTool) {
        dbTool.execSQL("Delete From OrderInvoice Where OrderDate<date('now','-14 day')");
        dbTool.execSQL("Delete From OrderInvoice Where OrderDate>date('now','-"+Days+" day')");
    }

    @Override
    public String getDataSql() {
        return "OrderInvoice"+(Days==3?"":Days);
    }

    @Override
    public String getSyncMethod() {
        return "getdataobjects";
    }

    @Override
    public int size() {
        return items.size();
    }

    @Override
    public DataSourceColumn getColumn(int i) {
        return (columns == null || columns.size() <= 0 || i < 0 || i >= columns.size())? null : columns.get(i);
    }

    @Override
    public int getColumnSize() {
        return columns.size();
    }

    @Override
    public String getColumnName(int i) {
        if (columns == null || columns.size() <= 0 || i < 0 || i >= columns.size()) return "";
        return columns.get(i).Name;
    }

    public int getColumnTextGravity(int i) {
        if (columns == null || columns.size() <= 0 || i < 0 || i >= columns.size()) return Gravity.NO_GRAVITY;
        return columns.get(i).TextGravity;
    }

    @Override
    public int getColumnWidth(int i) {
        if (columns == null || columns.size() <= 0 || i < 0 || i >= columns.size()) return 0;
        return columns.get(i).Width;
    }

    @Override
    public String getColumnFloatFormat(int i) {
        if (columns == null || columns.size() <= 0 || i < 0 || i >= columns.size()) return "";
        return columns.get(i).FloatFormat;
    }

    @Override
    public boolean moveToPosition(int i) {
        if (items == null || items.size() <= 0 || i < 0 || i >= items.size()) return false;
        Index = i;
        return true;
    }

    @Override
    public String getValue(int i, int i2) {
        if (items == null || items.size() <= 0 || i < 0 || i >= items.size()) return "";
        if (columns == null || columns.size() <= 0 || i2 < 0 || i2 >= columns.size()) return "";
        OrderInvoiceItem item = items.get(i);
        switch (i2) {
            case 0:
                return item.merchandise!=null?item.merchandise.ID:"";
            case 1:
                return item.merchandise!=null?item.merchandise.NameChinese:"";
            case 2:
                return item.Quantity / item.merchandise.Packing + "-" + item.Quantity % item.merchandise.Packing;
            case 3:
                return SalesType.getName(item.SalesType);
            case 4:
                return String.valueOf(item.Price);
            case 5:
                return item.merchandise!=null?String.valueOf(item.merchandise.Packing):"";
            default:
                return "";
        }
    }

    @Override
    public String getValue(int i) {
        return getValue(Index, i);
    }

    @Override
    public Object getObject(int i, int i1) {
        return null;
    }

    @Override
    public void setValue(int i, int i2, String s) {
        if (items == null || items.size() <= 0 || i < 0 || i >= items.size()) return;
        if (columns == null || columns.size() <= 0 || i2 < 0 || i2 >= columns.size()) return;
        OrderInvoiceItem item = items.get(i);
        switch (i2) {
            case 0:
                item.merchandise = Merchandise.getMerchandise(s);
                break;
            case 1:
//                item.merchandise.NameChinese = s;
                break;
            case 2:
                item.Quantity = Integer.valueOf(s);
                break;
            case 3:
                item.SalesType = s;
                break;
            case 4:
                item.Price = Float.parseFloat(s);
                break;
            default:
                break;
        }
    }

    @Override
    public void newDataRow() {
        items.add(new OrderInvoiceItem());
    }

    @Override
    public int getRowBgColor(int i){
        return Color.TRANSPARENT;
    }

    @Override
    public boolean getWidthIsPercent() {
        return false;
    }

    public void fullItem() {
        items.clear();
        Cursor cursor = DBTool.getInstance().query("Select * From OrderInvoiceItem Where BillNo="+ BillNo);
        OrderInvoiceItem orderInvoiceItem = new OrderInvoiceItem();
        Sum = Count = 0.0;
        while ((orderInvoiceItem = (OrderInvoiceItem)orderInvoiceItem.full(cursor))!= null){
            Sum += orderInvoiceItem.Price/orderInvoiceItem.merchandise.Packing*orderInvoiceItem.Quantity;
            Count += (float)orderInvoiceItem.Quantity / (float)orderInvoiceItem.merchandise.Packing;
            items.add(orderInvoiceItem);
        }
    }
}
